var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    buthiddenSubmit: false,
    buthiddenNext: false,
    name: '', //默认空
    idCard: '', //默认空
    phone: '',
    telephone: '',
    code: '', //6400
    verifyCodeTime: '发送验证码',
    timer: '',
    type: '' //1新增密码 2忘记密码
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    console.log(options) 
    that.setData({
      phone: options.phone.replace(/^(\d{3})\d{4}(\d+)/, '$1****$2'),
      telephone: options.phone,
      type: options.type,
      name: options.name
    })
  },
  // name: options.name,
  // idCard: options.idCard
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},
  //手机号前端校验
  onPhoneEvent: function(e) {
    var that = this
    if (e.detail.value.length == 1) {
      that.setData({ phone: e.detail.value.replace(/[^1-9]/g, '') })
    } else {
      that.setData({ phone: e.detail.value.replace(/\D/g, '') })
    }
  },
  bindName(e) {
    this.setData({ name: e.detail.value })
  },
  bindIdCard(e) {
    this.setData({ idCard: e.detail.value })
  },
  //手机号验证码前端校验
  onCodeEvent: function(e) {
    var that = this
    that.setData({ code: e.detail.value.replace(/\D/g, '') })
  },

  //提交手机号
  onPhoneNext: function() {
    var that = this
    var phone = that.data.telephone
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/
    var c = 60
    if (phone == '') {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none'
      })
      return false
    } else if (phone.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        duration: 1500
      })
      return false
    } else if (!myreg.test(phone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 1500
      })
      return false
    } else {
      that.setData({ buthiddenNext: true })
      that.data.timer = setInterval(function() {
        c = c - 1
        that.setData({
          verifyCodeTime: c + 's后重发'
        })
        if (c == 0) {
          clearInterval(that.data.timer)
          that.setData({
            verifyCodeTime: '获取验证码',
            buthiddenNext: false
          })
        }
      }, 1000)

      wx.request({
        method: 'POST',
        url: app.preUrl + 'obtainMessagePass',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          telephone: that.data.telephone
        },
        success: function(res) {
          console.log(res)
          if (res.data.code == 100) {
            that.setData({ buthiddenNext: true })
          } else {
            wx.showModal({
              title: res.data.message,
              confirmColor: '#56BDE0',
              showCancel: false
            })
            clearInterval(that.data.timer)
            that.setData({
              verifyCodeTime: '获取验证码',
              buthiddenNext: false
            })
          }
        },
        fail: function(error) {
          that.setData({ buthiddenNext: true })
        }
      })
    }
  },
  bindFormSubmit() {
    var that = this
    var name = that.data.name
    var idCard = that.data.idCard
    var phone = that.data.telephone
    var code = that.data.code
    var regName = /^[\u4e00-\u9fa5]{2,4}$/ //姓名校验
    var regIdNo = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/ //身份证校验
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/ //手机号校验
    if (!name || !idCard || !phone || !code) {
      wx.showToast({
        title: '各项信息必填',
        icon: 'none'
      })
      return false
    } else if (!regName.test(name)) {
      wx.showToast({
        title: '真实姓名有误',
        icon: 'none',
        duration: 1500
      })
      return false
    } else if (idCard.length != 18) {
      wx.showToast({
        title: '身份证长度有误',
        icon: 'none',
        duration: 1500
      })
      return false
    } else if (!regIdNo.test(idCard)) {
      wx.showToast({
        title: '身份证号有误',
        icon: 'none',
        duration: 1500
      })
      return false
    } else if (!myreg.test(phone)) {
      wx.showToast({
        title: '手机号有误',
        icon: 'none',
        duration: 1500
      })
      return false
    } else if (code.length != 4) {
      wx.showToast({
        title: '验证码长度有误',
        icon: 'none',
        duration: 1500
      })
      return false
    } else {
      that.setData({ buthiddenSubmit: true })
      wx.showLoading({ title: '校验中' })
      wx.request({
        method: 'POST',
        url: app.preUrl + 'checkPay',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          id: app.globalData.userId, //28444765309003157247057352550
          cardName: name,
          cardNum: idCard,
          telephone: phone,
          code: code
        },
        success: function(res) {
          wx.hideLoading()
          if (res.data.code == 100) {
            wx.showToast({
              title: '校验成功',
              icon: 'success',
              duration: 1000
            })
            setTimeout(function() {
              wx.navigateTo({
                url: 'setPaymentPassword?type=' + that.data.type
              })
            }, 1000)
          } else {
            wx.showModal({
              title: res.data.message,
              confirmColor: '#56BDE0',
              showCancel: false
            })
          }
          that.setData({ buthiddenNext: false, buthiddenSubmit: false })
        },
        fail: function(error) {
          that.setData({ buthiddenSubmit: false })
        }
      })
    }
  },
  onUnload() {
    //页面卸载  当redirectTo或navigateBack的时候调用
    clearInterval(this.data.timer)
  }
})
